package com.future.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.future.domain.Room;
import com.future.domain.Storey;
import com.future.dto.MsgDto;
import com.future.dto.StoreyDto;
import com.future.service.LoginService;
import com.future.service.StoreyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("storey")
public class StoreyController {

    @Autowired
    StoreyService storeyService;

    //实现分页查询所有楼栋(带 楼栋名模糊查询)
    @PostMapping("selectstorey")
    public Page<Storey> selectstorey(StoreyDto storeyDto){
        QueryWrapper queryWrapper = new QueryWrapper();

        queryWrapper.like("storeyName",storeyDto.getStoreyName());
        Page<Storey> page=new Page<>(storeyDto.getCurrent(),storeyDto.getSize());
        this.storeyService.page(page,queryWrapper);
        return page;


    }

    @PostMapping("updatestorey")
    public MsgDto updatestorey(@RequestBody Storey storey){



        List<Storey> list   =  this.storeyService.findRoomStatus(storey.getStoreyid());
        List<Room>  list1   =   list.get(0).getRooms();

        boolean  a    =   false;


        if(storey.getStoreystatus().equals("禁用")){

            for (int i = 0; i < list1.size(); i++) {
                if(list1.get(i).getRoomstatus().equals("启用")){

                    a  = false;
                }else{
                    System.out.println("禁用该楼栋成功");
                    a=true;
                }
            }

            if(a==false){
                System.out.println("有宿舍正在启用，无法禁用该楼栋");
                return  new MsgDto(false,"有宿舍正在启用，无法禁用该楼栋");
            }else{

                this.storeyService.updateById(storey);
                return  new MsgDto(true,"禁用该楼栋成功");

            }



        }else{

            this.storeyService.updateById(storey);
            return  new MsgDto(true,"修改成功");

        }





    }


}
